package com.pipayshop.controller;


import com.pipayshop.entity.dto.PayPalDTO;
import com.pipayshop.entity.vo.*;
import com.pipayshop.exception.BusinessException;
import com.pipayshop.service.TradinOrderService;
import com.pipayshop.service.TradinPostService;
import com.pipayshop.service.TradinRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.List;

/**
 * <p>
 * 用户账户表 前端控制器
 * </p>
 *
 * @author nws
 * @since 2023-07-25
 */
@Api(value = "用户账户接口", tags = "用户账户接口")
@RestController
@RequestMapping("/pipayshopapi/account-info")
public class TradeAccountInfoController {

    @Resource
    private TradinPostService tradinPostService;

    @Resource
    private TradinRateService tradinRateService;

    @Resource
    private TradinOrderService tradinOrderService;

    @PostMapping("publishTradition")
    @ApiOperation("发布交易")
    public ResponseVO publishTradition(String token) {
        try {
            boolean insert = tradinPostService.publishTradition(token);
            if (!insert) {
                throw new BusinessException();
            }
            return ResponseVO.getSuccessResponseVo("publication_success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("publication_failed");
        }
    }


    @GetMapping("selectTraditionList/{typeId}/{page}/{limit}")
    @ApiOperation("根据piName获取交易列表")
    public ResponseVO selectTraditionList(@PathVariable("typeId") Integer typeId,
                                          @PathVariable("page") Integer page,
                                          @PathVariable("limit") Integer limit,
                                          @RequestParam(required = false, defaultValue = "") String piName) {
        try {
            List<TraditionListVO> traditionVOS = tradinPostService.selectTraditionListByPiName(typeId, page, limit, piName);
            return ResponseVO.getSuccessResponseVo(traditionVOS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("data_retrieval_failed");
        }
    }

    @PutMapping("cancelTradition/{tradinId}/{piName}")
    @ApiOperation("取消交易")
    public ResponseVO<String> cancelTradition(@PathVariable("tradinId") String tradinId,
                                              @PathVariable("piName") String piName) {

        tradinPostService.cancelTradition(tradinId, piName);
        return ResponseVO.getSuccessResponseVo("取消交易成功");
    }

    @GetMapping("selectTraditionDetail/{tradinId}")
    @ApiOperation("获取交易详情")
    public ResponseVO selectTraditionDetail(@PathVariable("tradinId") String tradinId) {
        try {
            TraditionDetailVO traditionDetailVO = tradinPostService.selectTraditionDetail(tradinId);
            return ResponseVO.getSuccessResponseVo(traditionDetailVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("transaction_detail_retrieval_failed");
        }
    }

    @PostMapping("generateTradeOrder/{tradinId}/{buyerId}")
    @ApiOperation("生成交易订单")
    public ResponseVO<String> generateTradeOrder(@PathVariable("tradinId") String tradinId,
                                                 @PathVariable("buyerId") String buyerId) {
        try {
            String orderId = tradinOrderService.generateTradeOrder(tradinId, buyerId);
            return ResponseVO.getSuccessResponseVo(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("transaction_order_generation_failed");
        }
    }


    @PostMapping("upLoadPointBalance/{token}")
    @ApiOperation("提交积分")
    //todo 测试
    public ResponseVO upLoadPointBalance(@PathVariable("token") String token) {
        try {
            boolean insert = tradinOrderService.upLoadPointBalance(token);
            if (!insert) {
                throw new BusinessException("submit_transfer_points_failed");
            }

            return ResponseVO.getSuccessResponseVo("提交转积分成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("submit_transfer_points_failed");
        }
    }

    @PostMapping("upLoadImg/{token}")
    @ApiOperation("提交pi币转账凭证")
    //todo 测试
    public ResponseVO upLoadImg(@RequestBody MultipartFile file, @PathVariable("token") String token) {
        try {
            boolean insert = tradinOrderService.upLoadImg(file, token);
            if (!insert) {
                throw new BusinessException();
            }
            return ResponseVO.getSuccessResponseVo("提交凭证成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("submit_voucher_failed");
        }
    }

    @PostMapping("confirmTransaction/{token}")
    @ApiOperation("确认交易")
    //todo 测试
    public ResponseVO confirmTransaction(@PathVariable("token") String token) {
        try {
            boolean update = tradinOrderService.confirmTransaction(token);
            if (!update) {
                throw new BusinessException("confirm_transaction_failed");
            }
            return ResponseVO.getSuccessResponseVo("确认交易成功");
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("confirm_transaction_failed");
        }
    }


    @GetMapping("selectTradinPostByUid/{token}")
    @ApiOperation("获取用户交易列表")
    //todo 测试
    public ResponseVO selectTradinPostByUid(@PathVariable("token") String token) {
        try {
            List<TraditionListVO> traditionDTOList = tradinPostService.selectTradinPostByUid(token);
            return ResponseVO.getSuccessResponseVo(traditionDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("transaction_list_retrieval_failed");
        }
    }


    @GetMapping("selectTradinyOrderByUid")
    @ApiOperation("获取用户交易订单列表")
    public ResponseVO selectTradinyOrderByUid(@Param("token") String token) {
        try {
            List<TradinOrderListVO> tradinyOrderListVOS = tradinOrderService.selectTradinyOrderByUid(token);
            return ResponseVO.getSuccessResponseVo(tradinyOrderListVOS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("user_transaction_order_list_retrieval_failed");
        }
    }


    @GetMapping("selectTradinOrderDetail/{orderId}")
    @ApiOperation("交易双方获取交易订单详情接口")
    public ResponseVO selectTradinyOrderDetail(@PathVariable("orderId") String orderId) {
        try {
            TradinOrderDetailVO tradinOrderDetailVO = tradinOrderService.selectTradinOrderDetail(orderId);
            return ResponseVO.getSuccessResponseVo(tradinOrderDetailVO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("transaction_details_retrieval_failed");
        }
    }


    @GetMapping("selectAllTradinRate")
    @ApiOperation("获取汇率")
    public ResponseVO selectAllTradinRate() {
        try {

            List<TradinRateVO> tradinRateDTOList = tradinRateService.selectAllTradinRate();
            return ResponseVO.getSuccessResponseVo(tradinRateDTOList);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("exchange_rate_retrieval_failed");
        }
    }


    @PostMapping("/api/orders/{orderId}/capture")
    @ApiOperation("获取Payment")
    //todo 测试
    public Object capturePayment(@PathVariable("orderId") String orderId) {

        try {
            return tradinOrderService.capturePayment(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("payment_info_retrieval_failed");
        }

    }

    @PostMapping("/api/orders")
    @ApiOperation("生成订单")
    //todo 测试
    public Object createOrder(@RequestBody PayPalDTO payPalDTO) {
        try {
            return tradinOrderService.createOrder(payPalDTO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("order_generation_failed");
        }
    }
}
